package com.tensquare.gathering.controller;
import java.util.List;
import java.util.Map;

import com.tensquare.base.service.CityService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.tensquare.gathering.pojo.Gathering;
import com.tensquare.gathering.service.GatheringService;

import entity.PageResult;
import entity.Result;
import entity.StatusCode;

import javax.servlet.http.HttpServletRequest;

/**
 * 控制器层
 * @author Administrator
 *
 */
@RestController
@CrossOrigin
@RequestMapping("/gathering")
public class GatheringController {

	@Autowired
	private GatheringService gatheringService;
	@Autowired
	private CityService cityService;
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private RedisTemplate redisTemplate;
	
	/**
	 * 查询全部数据
	 * @return
	 */
	@RequestMapping(method= RequestMethod.GET)
	public Result findAll(){
		return new Result(true,StatusCode.OK,"查询成功",gatheringService.findAll());
	}
	
	/**
	 * 根据ID查询
	 * @param id ID
	 * @return
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.GET)
	public Result findById(@PathVariable String id){
		return new Result(true,StatusCode.OK,"查询成功",gatheringService.findById(id));
	}


	/**
	 * 分页+多条件查询
	 * @param searchMap 查询条件封装
	 * @param page 页码
	 * @param size 页大小
	 * @return 分页结果
	 */
	@RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
	public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
		/*将当前登录的用户id存入到缓存中*/
		Claims claims=(Claims)request.getAttribute("user_claims");
		if(claims!=null)
			redisTemplate.opsForValue().set( "uid_",claims.getId());
		else
			redisTemplate.delete("uid_");

		Page<Gathering> pageList = gatheringService.findSearch(searchMap, page, size);
		return  new Result(true,StatusCode.OK,"查询成功",  new PageResult<Gathering>(pageList.getTotalElements(), pageList.getContent()) );
	}
	@RequestMapping(value="/searchAll/{page}/{size}",method=RequestMethod.POST)
	public Result findSearcha(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
		Page<Gathering> pageList = gatheringService.findSearch(searchMap, page, size);
		List<Gathering> list=pageList.getContent();
		for(Gathering gathering:list){
			System.out.println(gathering.getName());
			String cityId=gathering.getCity();
			String name=cityService.getCityName(cityId);
			gathering.setCity(name);
		}
		return  new Result(true,StatusCode.OK,"查询成功",  new PageResult<Gathering>(pageList.getTotalElements(),list) );
	}

	/**
     * 根据条件查询
     * @param searchMap
     * @return
     */
    @RequestMapping(value="/search",method = RequestMethod.POST)
    public Result findSearch( @RequestBody Map searchMap){
        return new Result(true,StatusCode.OK,"查询成功",gatheringService.findSearch(searchMap));
    }
	
	/**
	 * 增加
	 * @param gathering
	 */
	@RequestMapping(method=RequestMethod.POST)
	public Result add(@RequestBody Gathering gathering  ){
		gatheringService.add(gathering);
		return new Result(true,StatusCode.OK,"增加成功");
	}
	
	/**
	 * 修改
	 * @param gathering
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.PUT)
	public Result update(@RequestBody Gathering gathering, @PathVariable String id ){
		gathering.setId(id);
		gatheringService.update(gathering);		
		return new Result(true,StatusCode.OK,"修改成功");
	}
	
	/**
	 * 删除
	 * @param id
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.DELETE)
	public Result delete(@PathVariable String id ){
		gatheringService.deleteById(id);
		return new Result(true,StatusCode.OK,"删除成功");
	}

	//根据城市查询活动并分页	gathering/city/{city}/{page}/{size}

	//活动报名
	@RequestMapping(value="/signup/{uid}/{gid}",method = RequestMethod.PUT)
	public Result signup(@PathVariable String uid,@PathVariable String gid){
		int flag=gatheringService.signup(uid,gid);
		return new Result(true,StatusCode.OK,"报名成功");
	}
	//取消活动报名
	@RequestMapping(value="/delsignup/{uid}/{gid}",method = RequestMethod.DELETE)
	public Result delsignup(@PathVariable String uid,@PathVariable String gid){
		int flag=gatheringService.delsignup(uid,gid);
		return new Result(true,StatusCode.OK,"取消报名成功");
	}
	//查看是否已经报名
	@RequestMapping(value="/signed/{gid}",method = RequestMethod.GET)
	public Result signed(@PathVariable String gid){
		//获取当前登录的用户id
		String uid= (String) redisTemplate.opsForValue().get("uid_");
		System.out.println("uid:"+uid+"\ngid:"+gid);
		int count=gatheringService.signed(uid,gid);
		//if(uid!=null) redisTemplate.delete("uid_");
		if(count>0)
			return new Result(true,StatusCode.OK,"您已经报名了");
		else
			return new Result(false,StatusCode.ERROR,"尚未报名");
	}

	//按时间排序查询最新的前四个的活动列表
	@RequestMapping(value = "/top",method = RequestMethod.GET)
	public Result getTop4(){
		List<Gathering> list=gatheringService.findTop4();
		return new Result(true,StatusCode.OK,"查询成功",list);
	}

	//查询我报名的活动列表
	@RequestMapping(value = "/myGatherings/{me}",method = RequestMethod.GET)
	public Result getMyGatherings(@PathVariable String me){
		List<String> list=gatheringService.getMyGatherings(me);
		return new Result(true,StatusCode.OK,"查询成功",list);
	}
}
